package com.wangzhen.spring.inject;

import com.wangzhen.spring.inject.holder.UserHolder;
import com.wangzhen.spring.ioc.dependcy.entity.User;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

/**
 * 注解方式演示：setter注入（手动注入）
 */
public class AnnotationSetterInjectDemo {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(AnnotationSetterInjectDemo.class);
        // 使用注解方式驱动的AnnotationConfigApplicationContext，也可以加载xml配置
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(applicationContext);
        reader.loadBeanDefinitions("classpath:/META-INF/dependcy-setter-context.xml");
        // 启动应用上下文
        applicationContext.refresh();
        // 获取UserHolder
        UserHolder userHolder = applicationContext.getBean(UserHolder.class);
        System.out.println(userHolder);
        // 关闭应用上下文
        applicationContext.close();

    }

    @Bean
    public UserHolder userHolder(User user) {
        UserHolder userHolder = new UserHolder();
        userHolder.setUser(user);
        return userHolder;
    }
}
